package com.example.mycontact

import android.content.Intent
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mycontact.DB.ContactTable
import com.example.mycontact.bean.Contact
import com.example.mycontact.databinding.ActivityInfoBinding
import java.io.ByteArrayInputStream

class InfoActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var binding: ActivityInfoBinding
    lateinit var contactTable: ContactTable
    var currentContact: Contact? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
        //接收传递参数id
        val intent = intent
        contactTable = ContactTable(this)
        val contact: Contact = intent.getSerializableExtra("contact1") as Contact
        currentContact = contact
        binding.tvName.text = contact.name
        binding.tvPhone.text = contact.tel
        binding.tvCompany.text = contact.company
        binding.tvPosition.text = contact.position
        
        // 显示头像
        if (contact.image.size > 0) {
            val stream = ByteArrayInputStream(contact.image)
            binding.ibTouxiang.setImageDrawable(Drawable.createFromStream(stream, "img"))
        }
        //打电话、发送短信、返回、编辑、关注按钮的事件监听
        binding.ibTel.setOnClickListener(this)
        binding.ibMsg.setOnClickListener(this)
        binding.ibBack.setOnClickListener(this)
        binding.ibEditTop.setOnClickListener(this)
        binding.ibFavorite.setOnClickListener(this)
        
        // 设置关注按钮的初始状态
        updateFavoriteButton()
    }

     override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ib_tel -> {
                //打电话
                val uri:Uri = Uri.parse("tel:" + binding.tvPhone.text.toString())
                val intenttel = android.content.Intent(Intent.ACTION_DIAL, uri)
                startActivity(intenttel)
            }
            R.id.ib_msg -> {
                //发短信，带上自带的短信内容
                val smsBody = "这是自带的短信内容"
                val uri:Uri=Uri.parse("smsto:" + binding.tvPhone.text.toString())
                val intentmsg = android.content.Intent(Intent.ACTION_SENDTO, uri)
                intentmsg.putExtra("sms_body", smsBody)
                startActivity(intentmsg)

            }
            R.id.ib_back -> {
                // 返回上一页
                finish()
            }
            R.id.ib_edit_top -> {
                // 打开编辑联系人页面
                currentContact?.let { contact ->
                    val editIntent = Intent(this, UpdateActivity::class.java)
                    editIntent.putExtra("updateContact", contact)
                    startActivity(editIntent)
                }
            }
            R.id.ib_favorite -> {
                // 切换关注状态
                currentContact?.let { contact ->
                    if (contactTable.toggleFavorite(contact.id)) {
                        contact.isFavorite = if (contact.isFavorite == 1) 0 else 1
                        updateFavoriteButton()
                    }
                }
            }
        }
    }
    
    // 更新关注按钮的图标
    private fun updateFavoriteButton() {
        currentContact?.let { contact ->
            if (contact.isFavorite == 1) {
                // 已关注状态，显示红心图标
                binding.ibFavorite.setImageResource(R.drawable.redh)
            } else {
                // 未关注状态，显示关注图标
                binding.ibFavorite.setImageResource(R.drawable.subscp)
            }
        }
    }
}